<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="`${type === 'Add' ? '新增' : '编辑'}更换记录`"
    width="700px"
    @close="handleClose"
    custom-class="dialog"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="易损件" prop="parts">
            <el-select v-model="form.parts" placeholder="请选择">
              <el-option v-for="item in partsOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更换时间" prop="replaceTime">
            <el-date-picker v-model="form.replaceTime" value-format="yyyy-MM-dd" type="date" placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="form.remark" :rows="6" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose" size="small">取 消</el-button>
      <el-button type="primary" @click="submitHandle" size="small">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addChange } from '@/api/maintenance';
export default {
  props: {
    partsOptions: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      type: 'Add',
      currentRow: null,
      dialogVisible: false,
      form: {
        parts: '',
        replaceTime: '',
        remark: '',
      },
      rules: {
        parts: [{ required: true, message: '请选择易损件', trigger: 'change' }],
        replaceTime: [{ required: true, message: '请选择更换时间', trigger: 'change' }],
      },
    };
  },
  methods: {
    openHandle(type, row) {
      this.type = type;
      this.dialogVisible = true;
      if (type === 'Edit') {
        this.setFormData(row);
      }
    },
    setFormData(row) {
      this.currentRow = { ...row };
      this.form.replaceTime = row.replaceTime;
      this.form.parts = row.parts;
      this.form.remark = row.remark;
    },
    handleClose() {
      this.$refs.form.resetFields();
      this.form = {
        parts: '',
        replaceTime: '',
        remark: '',
      };
      this.dialogVisible = false;
    },
    submitHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const submitParams = { ...this.form, projectId: this.$store.getters.projectId };
          if (this.type === 'Edit') {
            submitParams.id = this.currentRow.id;
          }
          addChange(submitParams).then(res => {
            if (res.success) {
              this.$message.success('操作成功');
              this.$emit('submit');
              this.handleClose();
            } else {
              this.$message.error('操作失败');
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.dialog {
  .el-date-editor {
    width: 100%;
  }
}
</style>
